package cn.sdhuijin.service;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.sdhuijin.dao.BranchDao;
import cn.sdhuijin.dao.UserDao;
import cn.sdhuijin.model.Branch;
import cn.sdhuijin.model.User;
import cn.sdhuijin.utils.CommonUtil;

/**
 * @author houyc
 *
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

	@Inject
	private UserDao userDao;
	@Inject
	private BranchDao branchDao;

	@Override
	public boolean addUser(User user) {
		boolean result = false;
		userDao.save(user);
		result = true;
		return result;
	}

	@Override
	public List<User> AllUsers() {
		return userDao.findAll();
	}

	@Override
	public boolean deleteUser(String selectId, String type) {
		boolean result = false;
		// 单个删除
		if ("single".equals(type)) {
			Long id = Long.parseLong(selectId);
			try {
				userDao.delete(id);
				result = true;
			} catch (Exception e) {
				result = false;
			}
			// 多个删除
		} else if ("multi".equals(type)) {
			String[] selectIdArray = selectId.split("@");
			for (int i = 0; i < selectIdArray.length; i++) {
				Long id = Long.parseLong(selectIdArray[i]);
				userDao.delete(id);
				result = true;
			}
		}
		return result;
	}

	@Override
	public User searchOne(String selectId) {
		Long id = Long.parseLong(selectId);
		return userDao.findOne(id);
	}

	@Override
	public Page<User> listUsers(int start, int limit, String userName,
			String userType, String branchId) {
		int page = start / limit;
		PageRequest pageRequest = new PageRequest(page, limit, new Sort(
				Direction.DESC, "branch"));
		if (userName != null && !"".equals(userName)) {
			userName = "%" + userName + "%";
		} else {
			userName = "%%";
		}
		if (userType != null && !"".equals(userType)) {
			if ("全部".equals(userType)) {
				userType = "%%";
			} else {
				userType = "%" + userType + "%";
			}
		} else {
			userType = "%%";
		}
		if (branchId != null && !"".equals(branchId)) {
			Branch branch = branchDao.findOne(Long.parseLong(branchId));
			List<Long> branchIds = new ArrayList<Long>();
			branchIds = CommonUtil.getBranchChildrenIds(branch);
			return userDao.findByNameLikeAndUserTypeLikeAndBranchIdIn(userName,
					userType, branchIds, pageRequest);
		} else {
			return userDao.findByNameLikeAndUserTypeLike(userName, userType,
					pageRequest);
		}
	}

	@Override
	public String restoreUserPass(String selectId) {
		String result = "";
		Long id = Long.parseLong(selectId);
		User user = userDao.findOne(id);
		if (user != null) {
			user.setPassword("88888888");
			result = user.getName();
		}
		userDao.save(user);
		return result;
	}

	@Override
	public boolean saveUserInfo(String selectId, String userType, String sex,
			String age, String email, String telphone) {
		boolean result = false;
		try {
			User user = userDao.findOne(Long.parseLong(selectId));
			user.setUserType(userType);
			user.setAge(age);
			user.setEmail(email);
			user.setSex(sex);
			user.setTelphone(telphone);
			userDao.save(user);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public User isExist(String telphone, String password) {
		List<User> users = userDao
				.findByTelphoneAndPassword(telphone, password);
		User user = null;
		if (users.size() > 0) {
			user = users.get(0);
		}
		return user;
	}

	@Override
	public boolean confrimUserPasswordById(Long id, String password) {
		boolean result = false;
		User user = userDao.findOne(id);
		if (user.getPassword() != null && password != null) {
			result = password.equals(user.getPassword());
		}
		return result;
	}

	@Override
	public boolean modifyUserPassword(Long id, String initPassword) {
		boolean result = false;
		User user = userDao.findOne(id);
		user.setPassword(initPassword);
		userDao.save(user);
		result = true;
		return result;
	}

	@Override
	public User getCurrentUserInfo(Long id) {
		return userDao.findOne(id);
	}

	@Override
	public boolean modifyUserInfo(String selectId, String sex, String age,
			String email, String telphone) {
		boolean result = false;
		try {
			User user = userDao.findOne(Long.parseLong(selectId));
			user.setAge(age);
			user.setEmail(email);
			user.setSex(sex);
			user.setTelphone(telphone);
			userDao.save(user);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public User isExistByTelephone(String telephone) {
		List<User> users = userDao.findByTelphone(telephone);
		User user = null;
		if (users.size() > 0) {
			user = users.get(0);
		}
		return user;
	}

	@Override
	public List<User> getLoanManagerList(Long branchId, String userType) {
		List<Long> branchIds = new ArrayList<Long>();
		branchIds.add(branchId);
		return userDao.findByBranchIdInAndUserTypeLike(branchIds, userType
				+ "%");
	}

	@Override
	public List<User> ExportList(String name, String userType, String branchId) {
		if (CommonUtil.allIsNotEmpty(name)) {
			name = "%" + name + "%";
		} else {
			name = "%%";
		}
		if (CommonUtil.allIsNotEmpty(userType)) {
			if ("全部".equals(userType)) {
				userType = "%%";
			} else {
				userType = "%" + userType + "%";
			}
		} else {
			userType = "%%";
		}
		if (CommonUtil.allIsNotEmpty(branchId)) {
			Branch branch = branchDao.findOne(Long.parseLong(branchId));
			List<Long> branchIds = new ArrayList<Long>();
			branchIds = CommonUtil.getBranchChildrenIds(branch);
			return userDao.findByNameLikeAndUserTypeLikeAndBranchIdIn(name,
					userType, branchIds);
		} else {
			return userDao.findByNameLikeAndUserTypeLike(name, userType);
		}
	}
}
